package com.scisoft.scan.control.depot;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.scisoft.scan.dy.R;
import com.scisoft.scan.data.depot.DepotOrderBean;

import java.util.List;

public class DepotInfoAdapter extends BaseAdapter {
    private List<DepotOrderBean> depotOrderBeanList;//数据源
    private LayoutInflater mInflater;//布局装载器对象
    private Context mContext;

    public DepotInfoAdapter(List<DepotOrderBean> depotOrderBeanList, Context context) {
        this.depotOrderBeanList = depotOrderBeanList;
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        //return返回的是int类型，也就是页面要显示的数量。
        return depotOrderBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return depotOrderBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将布局文件转化为View对象
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_depotorder, null);
        }
        //作业类型
        TextView tv_orderType = convertView.findViewById(R.id.tv_orderType);
        //拣货单号
        TextView tv_no = convertView.findViewById(R.id.tv_no);
        //订单编号
        TextView tv_orderNo = convertView.findViewById(R.id.tv_orderNo);
        //时间
        TextView tv_optime = convertView.findViewById(R.id.tv_optime);
        //订单信息
        TextView tv_orderInfo = convertView.findViewById(R.id.tv_orderInfo);
        //状态
        TextView tv_orderState = convertView.findViewById(R.id.tv_orderState);
        //操作员
        TextView tv_operator = convertView.findViewById(R.id.tv_operator);
        //仓库
        TextView tv_depotName = convertView.findViewById(R.id.tv_depotName);
        //库区
        TextView tv_areaName = convertView.findViewById(R.id.tv_areaName);
        //供应商
        TextView tv_supplierName = convertView.findViewById(R.id.tv_supplierName);
        //包装区域
        TextView tv_packArea = convertView.findViewById(R.id.tv_packArea);
        //金额
        TextView tv_amount = convertView.findViewById(R.id.tv_amount);
        //类型
        TextView tv_orderTypeName = convertView.findViewById(R.id.tv_orderTypeName);

        //获取相应索引的ItemBean对象
        DepotOrderBean bean = depotOrderBeanList.get(position);

        //设置控件的对应属性值
        String orderTypeName = "未知";
        if ("1".equals(bean.orderType)) {
            orderTypeName = "出仓";
            tv_orderType.setTextColor(Color.parseColor("#00c103"));
            tv_orderType.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_stat_green));
            tv_orderType.setPadding(10, 2, 10, 2);
            //包装区域，出仓有效
            tv_packArea.setVisibility(View.VISIBLE);
            if ("".equals(bean.packArea) || bean.packArea == null) {
                tv_packArea.setText("暂未录入");
            } else {
                tv_packArea.setText("包装区域:" + bean.packArea);
            }
        } else if ("2".equals(bean.orderType)) {
            orderTypeName = "入仓";
            tv_orderType.setTextColor(Color.parseColor("#cc0a00"));
            tv_orderType.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_stat_red));
            tv_orderType.setPadding(10, 2, 10, 2);
            //包装区域，入仓无效
            tv_packArea.setVisibility(View.INVISIBLE);

        }
        tv_orderType.setText(orderTypeName);
        tv_no.setText(bean.no);

        tv_orderNo.setText(bean.orderNo);
        tv_optime.setText(bean.optime);
        tv_orderInfo.setText(bean.orderInfo);
        tv_orderState.setText(bean.orderState);
        tv_operator.setText(bean.operator);
        tv_depotName.setText(bean.depotName);
        tv_areaName.setText(bean.areaName);
        tv_supplierName.setText(bean.supplierName);
        tv_amount.setText(bean.amount);
        tv_orderTypeName.setText(bean.orderTypeName);
        if (bean.orderTypeName != null && bean.orderTypeName.contains("急件")) {
            tv_orderTypeName.setTextColor(Color.parseColor("#FF0000"));
        } else if (bean.orderTypeName != null && bean.orderTypeName.contains("计划")) {
            tv_orderTypeName.setTextColor(Color.parseColor("#0000FF"));
        } else {
            tv_orderTypeName.setTextColor(Color.parseColor("#999999"));
        }

        return convertView;
    }
}
